package cn.edu.zjicm.wordsnet_d.activity;

import java.util.Calendar;

import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import cn.edu.zjicm.wordsnet_d.R;
import cn.edu.zjicm.wordsnet_d.activity.base.WordBaseActivity;
import cn.edu.zjicm.wordsnet_d.data.DataManager;
import cn.edu.zjicm.wordsnet_d.data.StaticData;
import cn.edu.zjicm.wordsnet_d.data.StudyPlan;
import cn.edu.zjicm.wordsnet_d.datapersistent.AppPreference;
import cn.edu.zjicm.wordsnet_d.datapersistent.AppPreference.BookType;
import cn.edu.zjicm.wordsnet_d.datapersistent.FileManager;
import cn.edu.zjicm.wordsnet_d.db.WordFactory;
import cn.edu.zjicm.wordsnet_d.download.DownloadManager;
import cn.edu.zjicm.wordsnet_d.util.DateTimeUtils;
import cn.edu.zjicm.wordsnet_d.util.LogUtil;
import cn.edu.zjicm.wordsnet_d.view.FakeCheckBox;
import cn.edu.zjicm.wordsnet_d.view.MyDialog;

public class SettingPlanActivity extends WordBaseActivity implements OnClickListener {

	public static void startActivity(Context mContext, int startMode, String bookTvStr, StudyPlan studyPlan) { // 这是设置学习计划
		Intent in = new Intent(mContext, SettingPlanActivity.class);
		in.putExtra("startMode", startMode);
		in.putExtra("bookTvStr", bookTvStr);
		in.putExtra("studyPlan", studyPlan);
		mContext.startActivity(in);
	}

	public static void startActivity(Context mContext, int startMode) {
		Intent in = new Intent(mContext, SettingPlanActivity.class);
		in.putExtra("startMode", startMode);
		mContext.startActivity(in);
	}

	private int startMode;
	private int unlearnedWordNum; // 未学单词总数,MODE_INIT模式用
	private int everyDayNum; // 每天学习单词总数,MODE_INIT模式用
	private int wordAllCount = 0, phraseAllCount = 0, wordPerCount = 0, phrasePerCount = 0;
	private String wordBookNameStr, phraseBookNameStr; // 选择的单词书名和词组书名
	private boolean wordBookIsChecked, phraseBookIsChecked;

	// layout1
	private LinearLayout detail_layout;
	private TextView bookTv, detailTv, allCountTv, everyDayCountTv, detailPlanChange;
	private Button startBtn;
	// layout2
	private TextView studyingBookTv; // 右上角列出正在学单词书
	private LinearLayout wordBookLayout1, wordBookLayout2, phraseBookLayout1, phraseBookLayout2;
	private TextView wordBook, wordBookName, phraseBook, phraseBookName; // 单词书名和词组书名
	private TextView wordBookDetail, wordBookTv0, wordBookTv1, wordBookPlanChange, phraseBookDetail, phraseBookTv0, phraseBookTv1, phraseBookPlanChange; // 截止日期等详细信息
	private ImageView wordBookArrow, phraseBookArrow;
	private Button sureBtn;
	private FakeCheckBox wordCheckBox, phraseCheckBox;

	private WordFactory mFactory;
	private DownloadManager downloadManager;
	private StudyPlan wordStudyPlan = null, phraseStudyPlan = null;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		startMode = getIntent().getIntExtra("startMode", 0);
		setTitleBarStyle(WordBaseActivity.TITLE_BAR_STYLE_BACK_BUTTON);
		if (startMode == StaticData.MODE_INIT) {
			setContentView(R.layout.activity_setting_plan2_layout1);
			setTitleText("开始学习");
		} else {
			setContentView(R.layout.activity_setting_plan2_layout2);
			setTitleText("更换学习计划");
			setRightButton1("", R.drawable.calendar_info_selector, showHintDialogListener);
		}
		findView();
		initView();
	}

	OnClickListener showHintDialogListener = new OnClickListener() {

		@Override
		public void onClick(View v) {
			showHintDialog();
		}
	};

	private void findView() {
		allCountTv = (TextView) findViewById(R.id.allCount_tv);
		everyDayCountTv = (TextView) findViewById(R.id.everyDayCount_tv);

		// layout1
		if (startMode == StaticData.MODE_INIT) {
			detail_layout = (LinearLayout) findViewById(R.id.detail_layout);
			bookTv = (TextView) findViewById(R.id.book_tv);
			detailTv = (TextView) findViewById(R.id.detail_tv);
			startBtn = (Button) findViewById(R.id.start_study_btn);
			detailPlanChange = (TextView) findViewById(R.id.detail_plan_change);
		} else { // layout2
			studyingBookTv = (TextView) findViewById(R.id.studying_book_tv);
			wordBookLayout1 = (LinearLayout) findViewById(R.id.word_book_layout1);
			wordBookLayout2 = (LinearLayout) findViewById(R.id.word_book_layout2);
			phraseBookLayout1 = (LinearLayout) findViewById(R.id.phrase_book_layout1);
			phraseBookLayout2 = (LinearLayout) findViewById(R.id.phrase_book_layout2);
			wordBook = (TextView) findViewById(R.id.word_book);
			wordBookName = (TextView) findViewById(R.id.word_book_name);
			phraseBook = (TextView) findViewById(R.id.phrase_book);
			phraseBookName = (TextView) findViewById(R.id.phrase_book_name);
			wordBookArrow = (ImageView) findViewById(R.id.word_book_arrow);
			wordBookDetail = (TextView) findViewById(R.id.word_book_detail);
			wordBookTv0 = (TextView) findViewById(R.id.word_book_tv0);
			wordBookTv1 = (TextView) findViewById(R.id.word_book_tv1);
			wordBookPlanChange = (TextView) findViewById(R.id.word_book_plan_change);
			phraseBookArrow = (ImageView) findViewById(R.id.phrase_book_arrow);
			phraseBookDetail = (TextView) findViewById(R.id.phrase_book_detail);
			phraseBookTv0 = (TextView) findViewById(R.id.phrase_book_tv0);
			phraseBookTv1 = (TextView) findViewById(R.id.phrase_book_tv1);
			phraseBookPlanChange = (TextView) findViewById(R.id.phrase_book_plan_change);
			sureBtn = (Button) findViewById(R.id.sure_btn);
			wordCheckBox = (FakeCheckBox) findViewById(R.id.word_cb);
			phraseCheckBox = (FakeCheckBox) findViewById(R.id.phrase_cb);
		}
	}

	private void initView() {
		mFactory = WordFactory.getInstance(this);
		// greenColor = getResources().getColor(R.color.progress_bar);
		// orangeColor = getResources().getColor(R.color.app_orange);
		downloadManager = new DownloadManager(this, handler);

		if (startMode == StaticData.MODE_INIT) {
			initViewForModeInit();
		} else {
			initViewForModeUpdate();
		}
	}

	private void initViewForModeInit() {

		final StudyPlan studyPlan = (StudyPlan) getIntent().getSerializableExtra("studyPlan");
		unlearnedWordNum = mFactory.getUnLearnedWordCount(studyPlan.getBookIndex());

		String bookTvStr = getIntent().getStringExtra("bookTvStr");
		if (bookTvStr != null && bookTvStr.length() > 0) {
			bookTv.setText(bookTvStr);
		}

		if (studyPlan.getStudyMode() == AppPreference.StudyMode.MODE_END_TIME) {
			// detailTv.setBackgroundColor(greenColor);
			detailTv.setText(getEndTimeText(studyPlan));
			detail_layout.setBackgroundResource(R.drawable.learnplan_change_bg0);
		} else {
			// detailTv.setBackgroundColor(whiteColor);
			detailTv.setText(getEveryDayNumText(studyPlan, unlearnedWordNum));
			detail_layout.setBackgroundResource(R.drawable.learnplan_change_bg2);
		}

		detailPlanChange.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				finish();
			}
		});

		allCountTv.setText(unlearnedWordNum + "");
		everyDayCountTv.setText(everyDayNum + "");

		startBtn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				startBtnClick(studyPlan);
			}
		});
	}

	private void setEndTimeText(StudyPlan studyPlan, int type) {
		StringBuilder sb = new StringBuilder();
		everyDayNum = getEveryDayNumber(studyPlan.getBookIndex(), studyPlan.getEndYear(), studyPlan.getEndMonth(), studyPlan.getEndDay());
		sb.append(studyPlan.getEndYear());
		sb.append("-");
		sb.append(studyPlan.getEndMonth() + 1);
		sb.append("-");
		sb.append(studyPlan.getEndDay());

		if (type == BookType.BOOK_WORD) {
			wordBookDetail.setText(sb.toString());
			wordBookTv0.setText("截止");
			wordBookTv1.setText("预计每日学习新词 " + everyDayNum + " 个");
		} else {
			phraseBookDetail.setText(sb.toString());
			phraseBookTv0.setText("截止");
			phraseBookTv1.setText("预计每日学习新词 " + everyDayNum + " 个");
		}
	}

	private void setEveryDayNumText(StudyPlan studyPlan, int unlearnedCount, int type) {
		everyDayNum = studyPlan.getEveryDayNumber();
		if (type == BookType.BOOK_WORD) {
			wordBookDetail.setText(studyPlan.getEveryDayNumber() + "");
			wordBookTv0.setText("个/日");
			wordBookTv1.setText("预计 " + getEndTimeByEveryDayNum(unlearnedCount, studyPlan.getEveryDayNumber()) + " 完成");
		} else {
			phraseBookDetail.setText(studyPlan.getEveryDayNumber() + "");
			phraseBookTv0.setText("个/日");
			phraseBookTv1.setText("预计 " + getEndTimeByEveryDayNum(unlearnedCount, studyPlan.getEveryDayNumber()) + " 完成");
		}
	}

	private String getEndTimeText(StudyPlan studyPlan) {
		StringBuilder sb = new StringBuilder();
		everyDayNum = getEveryDayNumber(studyPlan.getBookIndex(), studyPlan.getEndYear(), studyPlan.getEndMonth(), studyPlan.getEndDay());
		sb.append(studyPlan.getEndYear());
		sb.append("-");
		sb.append(studyPlan.getEndMonth() + 1);
		sb.append("-");
		sb.append(studyPlan.getEndDay());
		sb.append("截止\n预计每日学习新词");
		sb.append(everyDayNum);
		sb.append("个");
		return sb.toString();
	}

	private String getEveryDayNumText(StudyPlan studyPlan, int unlearnedCount) {
		StringBuilder sb = new StringBuilder();
		everyDayNum = studyPlan.getEveryDayNumber();
		sb.append(studyPlan.getEveryDayNumber());
		sb.append("个/日\n预计");
		sb.append(getEndTimeByEveryDayNum(unlearnedCount, studyPlan.getEveryDayNumber()));
		sb.append("完成");
		return sb.toString();
	}

	/**
	 * 根据截止日期计算每天学习个数
	 * 
	 * @param bookIndex
	 * @param year
	 * @param monthOfYear
	 * @param dayOfMonth
	 * @return
	 */
	private int getEveryDayNumber(int bookIndex, int year, int monthOfYear, int dayOfMonth) {
		int unlearnNum = mFactory.getUnLearnedWordCount(bookIndex);
		int wordsNumPerDay = DateTimeUtils.getWordsNumberPerDayForPlan2(unlearnNum, this, year, monthOfYear, dayOfMonth);
		LogUtil.d("unlearnNum=" + unlearnNum + ",wordsNumPerDay=" + wordsNumPerDay);
		return wordsNumPerDay;
	}

	/**
	 * 根据每天学习个数推算出结束时间
	 * 
	 * @param bookIndex
	 * @param everyDayNumber
	 * @return
	 */
	private String getEndTimeByEveryDayNum(int unlearnedCount, int everyDayNumber) {
		Calendar calendar = DateTimeUtils.getEndTimeByEveryDayNum(unlearnedCount, everyDayNumber);
		StringBuilder sb = new StringBuilder();
		sb.append(calendar.get(Calendar.YEAR));
		sb.append("-");
		sb.append(calendar.get(Calendar.MONTH) + 1);
		sb.append("-");
		sb.append(calendar.get(Calendar.DAY_OF_MONTH));
		return sb.toString();
	}

	/**
	 * MODE_INIT时，点击开始学习按钮
	 * 
	 * @param studyPlan
	 */
	protected void startBtnClick(StudyPlan studyPlan) {
		handler.sendMessage(handler.obtainMessage(0));
		studyPlan.setLearning(true);
		DataManager.getInstance().addStudyPlan(this, studyPlan);
		handler.sendMessage(handler.obtainMessage(1));
		AppPreference.setFirstGuidePage(this, false);
	}

	private Handler handler = new Handler() {
		private ProgressDialog dialog2 = null;

		public void handleMessage(android.os.Message msg) {
			switch (msg.what) {
			case 0:
				dialog2 = new ProgressDialog(SettingPlanActivity.this);
				dialog2.setCancelable(false);
				dialog2.setCanceledOnTouchOutside(false);
				dialog2.show();
				dialog2.setContentView(R.layout.customprogressdialog);
				break;
			case 1:
				dialog2.dismiss();
				askDownload();
				break;
			case 2:
				startNextActivity();
				break;
			}
		};
	};

	void askDownload() {
		if (FileManager.hasNotDownloadedVoicePack(this, false)) { // 判断压缩包是否存在
			downloadManager.showAskDownloadWindow();
		} else {
			startNextActivity();
		}
	}

	public void startNextActivity() {
		if (SelectStudyModeActivity.mContext != null) {
			SelectStudyModeActivity.mContext.toFinish();
		}
		if (SelectBookActivity.mInstance != null) {
			SelectBookActivity.mInstance.finishActivity();
		}
		if (LoginActivity.mLoginActivity != null) {
			LoginActivity.mLoginActivity.toFinish();
		}
		if (RegisterActivity.mContext != null) {
			RegisterActivity.mContext.toFinish();
		}
		finish();
		MainActivity.startActivity(SettingPlanActivity.this);
	}

	private void initViewForModeUpdate() {
		wordStudyPlan = StudyPlan.getWordStudyPlan(this);
		phraseStudyPlan = StudyPlan.getPhraseStudyPlan(this);
		initWordStudyPlan();
		initPhraseStudyPlan();
		update();
		sureBtn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				sureBtnClick();
			}
		});
	}

	private void update() {
		initStudyingBookTv();
		int allCount = 0, everyDayCount = 0;
		if (wordBookIsChecked) {
			allCount += wordAllCount;
			everyDayCount += wordPerCount;
		}

		if (phraseBookIsChecked) {
			allCount += phraseAllCount;
			everyDayCount += phrasePerCount;
		}
		allCountTv.setText(allCount + "");
		everyDayCountTv.setText(everyDayCount + "");
		setSureBtnState();
	}

	private void initStudyingBookTv() {
		StringBuilder sb = new StringBuilder();
		boolean hasWordBook = false, hasPhraseBook = false;
		int len = 0;

		if (wordBookNameStr != null && wordBookNameStr.length() > 0 && wordCheckBox.isChecked()) {
			hasWordBook = true;
			len++;
		}
		if (phraseBookNameStr != null && phraseBookNameStr.length() > 0 && phraseCheckBox.isChecked()) {
			hasPhraseBook = true;
			len++;
		}

		if (len == 0) {
			sb.append("无");
		} else if (len == 1) {
			sb.append("仅");
			if (hasWordBook)
				sb.append(wordBookNameStr);
			else if (hasPhraseBook)
				sb.append(phraseBookNameStr);
		} else {
			sb.append(wordBookNameStr);
			sb.append(",");
			sb.append(phraseBookNameStr);
		}

		studyingBookTv.setText(sb.toString());
	}

	private void initWordStudyPlan() {
		if (wordStudyPlan != null) {

			if (wordStudyPlan.isLearning()) {
				wordBookLayout2.setVisibility(View.VISIBLE);
				wordCheckBox.setChecked(true);
			} else {
				wordBookLayout2.setVisibility(View.GONE);
				wordCheckBox.setChecked(false);
			}

			wordAllCount = mFactory.getUnLearnedWordCount(wordStudyPlan.getBookIndex());
			StringBuilder sb = new StringBuilder();
			wordBookNameStr = mFactory.getBookName(AppPreference.Book.BOOK_ID[wordStudyPlan.getBookIndex()]);
			sb.append(wordBookNameStr);
			sb.append("(");
			sb.append(wordAllCount);
			sb.append("/");
			sb.append(mFactory.getWordCountInBook(wordStudyPlan.getBookIndex()));
			sb.append(")");
			wordBookName.setText(sb.toString());

			if (wordStudyPlan.getStudyMode() == AppPreference.StudyMode.MODE_END_TIME) {
				// wordBookDetail.setBackgroundColor(greenColor);
				// wordBookDetail.setText(getEndTimeText(wordStudyPlan));
				setEndTimeText(wordStudyPlan, BookType.BOOK_WORD);
			} else {
				// wordBookDetail.setBackgroundColor(orangeColor);
				// wordBookDetail.setText(getEveryDayNumText(wordStudyPlan,
				// wordAllCount));
				setEveryDayNumText(wordStudyPlan, wordAllCount, BookType.BOOK_WORD);
			}
			LogUtil.d("initWordStudyPlan()中,everyDayNum=" + everyDayNum);
			wordPerCount = everyDayNum;

		} else {
			wordBookLayout2.setVisibility(View.GONE);
			wordBookName.setText("暂时不需要");
			wordCheckBox.setChecked(false);
		}

		wordBookLayout1.setOnClickListener(this);
		wordBookPlanChange.setOnClickListener(this);
		wordBookIsChecked = wordCheckBox.isChecked();

		setWordBookLayout1();
		wordCheckBox.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				if (wordStudyPlan != null) {
					wordBookIsChecked = !wordCheckBox.isChecked();
					wordStudyPlan.setLearning(wordBookIsChecked);

					wordCheckBox.setChecked(wordBookIsChecked);
					setWordBookLayout1();
					update();
				} else {
					changeWordBook();
				}
				setSureBtnState();
				initStudyingBookTv();
			}
		});
	}

	private void setWordBookLayout1() {
		if (wordBookIsChecked) {
			if (wordStudyPlan.getStudyMode() == AppPreference.StudyMode.MODE_END_TIME) {
				TypedValue typedValue = new TypedValue();
				getTheme().resolveAttribute(R.attr.learnplan_change_bg0, typedValue, true);
				wordBookLayout1.setBackgroundResource(typedValue.resourceId);
			} else {
				TypedValue typedValue = new TypedValue();
				getTheme().resolveAttribute(R.attr.learnplan_change_bg2, typedValue, true);
				wordBookLayout1.setBackgroundResource(typedValue.resourceId);
			}
			wordBookLayout2.setVisibility(View.VISIBLE);
			wordBook.setTextColor(Color.WHITE);
			wordBookName.setTextColor(Color.WHITE);
			wordBookArrow.setImageResource(R.drawable.arrow_right_white);

		} else {
			TypedValue typedValue = new TypedValue();
			getTheme().resolveAttribute(R.attr.learnplan_change_bg1, typedValue, true);
			wordBookLayout1.setBackgroundResource(typedValue.resourceId);
			wordBookLayout2.setVisibility(View.GONE);
			wordBook.setTextColor(Color.parseColor("#666666"));
			wordBookName.setTextColor(Color.parseColor("#666666"));
			wordBookArrow.setImageResource(R.drawable.setting_selecte);
		}
	}

	private void initPhraseStudyPlan() {
		if (phraseStudyPlan != null) {

			if (phraseStudyPlan.isLearning()) {
				phraseBookLayout2.setVisibility(View.VISIBLE);
				phraseCheckBox.setChecked(true);
			} else {
				phraseBookLayout2.setVisibility(View.GONE);
				phraseCheckBox.setChecked(false);
			}

			phraseAllCount = mFactory.getUnLearnedWordCount(phraseStudyPlan.getBookIndex());
			StringBuilder sb = new StringBuilder();
			phraseBookNameStr = mFactory.getBookName(AppPreference.Book.BOOK_ID[phraseStudyPlan.getBookIndex()]);
			sb.append(phraseBookNameStr);
			sb.append("(");
			sb.append(phraseAllCount);
			sb.append("/");
			sb.append(mFactory.getWordCountInBook(phraseStudyPlan.getBookIndex()));
			sb.append(")");
			phraseBookName.setText(sb.toString());

			if (phraseStudyPlan.getStudyMode() == AppPreference.StudyMode.MODE_END_TIME) {
				setEndTimeText(phraseStudyPlan, BookType.BOOK_PHRASE);
			} else {
				setEveryDayNumText(phraseStudyPlan, phraseAllCount, BookType.BOOK_PHRASE);
			}
			LogUtil.d("initPhraseStudyPlan()中,everyDayNum=" + everyDayNum);
			phrasePerCount = everyDayNum;

		} else {
			phraseBookLayout2.setVisibility(View.GONE);
			phraseBookName.setText("暂时不需要");
			phraseCheckBox.setChecked(false);
		}

		phraseBookLayout1.setOnClickListener(this);
		phraseBookPlanChange.setOnClickListener(this);
		phraseBookIsChecked = phraseCheckBox.isChecked();

		setPhraseBookLayout1();
		phraseCheckBox.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				if (phraseStudyPlan != null) {
					phraseBookIsChecked = !phraseCheckBox.isChecked();
					phraseStudyPlan.setLearning(phraseBookIsChecked);

					phraseCheckBox.setChecked(phraseBookIsChecked);
					setPhraseBookLayout1();
					update();
				} else {
					changePhraseBook();
				}
				setSureBtnState();
				initStudyingBookTv();
			}
		});
	}

	private void setPhraseBookLayout1() {

		if (phraseBookIsChecked) {
			if (phraseStudyPlan.getStudyMode() == AppPreference.StudyMode.MODE_END_TIME) {
				TypedValue typedValue = new TypedValue();
				getTheme().resolveAttribute(R.attr.learnplan_change_bg0, typedValue, true);
				phraseBookLayout1.setBackgroundResource(typedValue.resourceId);

			} else {
				TypedValue typedValue = new TypedValue();
				getTheme().resolveAttribute(R.attr.learnplan_change_bg2, typedValue, true);
				phraseBookLayout1.setBackgroundResource(typedValue.resourceId);
			}
			phraseBookLayout2.setVisibility(View.VISIBLE);
			phraseBook.setTextColor(Color.WHITE);
			phraseBookName.setTextColor(Color.WHITE);
			phraseBookArrow.setImageResource(R.drawable.arrow_right_white);

		} else {
			TypedValue typedValue = new TypedValue();
			getTheme().resolveAttribute(R.attr.learnplan_change_bg1, typedValue, true);
			phraseBookLayout1.setBackgroundResource(typedValue.resourceId);
			phraseBookLayout2.setVisibility(View.GONE);
			phraseBook.setTextColor(Color.parseColor("#666666"));
			phraseBookName.setTextColor(Color.parseColor("#666666"));
			phraseBookArrow.setImageResource(R.drawable.setting_selecte);
		}
	}

	private void setSureBtnState() {
		if (wordBookIsChecked || phraseBookIsChecked) {
			sureBtn.setEnabled(true);
		} else {
			sureBtn.setEnabled(false);
		}
	}

	@Override
	public void onClick(View v) {
		if (v == sureBtn) { // 点击layout2中的确定按钮
			sureBtnClick();
		} else if (v == wordBookLayout1) { //
			changeWordBook();
		} else if (v == wordBookPlanChange) {
			changeWordPlan();
		} else if (v == phraseBookLayout1) {
			changePhraseBook();
		} else if (v == phraseBookPlanChange) {
			changePhrasePlan();
		}
	}

	/**
	 * 更改单词书计划
	 */
	private void changeWordPlan() {
		Intent in = new Intent(this, SelectStudyModeActivity.class);
		in.putExtra("bookIndex", wordStudyPlan.getBookIndex());
		in.putExtra("startMode", StaticData.MODE_UPDATE);
		in.putExtra("bookType", BookType.BOOK_WORD);
		startActivityForResult(in, StaticData.requestCode);
	}

	/**
	 * 更改词组书计划
	 */
	private void changePhrasePlan() {
		Intent in = new Intent(this, SelectStudyModeActivity.class);
		in.putExtra("bookIndex", phraseStudyPlan.getBookIndex());
		in.putExtra("startMode", StaticData.MODE_UPDATE);
		in.putExtra("bookType", BookType.BOOK_PHRASE);
		startActivityForResult(in, StaticData.requestCode);
	}

	/**
	 * 修改词组书
	 */
	private void changePhraseBook() {
		Intent in = new Intent(this, SelectBookActivity.class);
		in.putExtra("startMode", StaticData.MODE_UPDATE);
		in.putExtra("bookType", AppPreference.BookType.BOOK_PHRASE);
		startActivityForResult(in, StaticData.requestCode);
	}

	/**
	 * 修改单词书
	 */
	private void changeWordBook() {
		Intent in = new Intent(this, SelectBookActivity.class);
		in.putExtra("startMode", StaticData.MODE_UPDATE);
		in.putExtra("bookType", AppPreference.BookType.BOOK_WORD);
		startActivityForResult(in, StaticData.requestCode);
	}

	/**
	 * MODE_UPDATE模式时,点击确定按钮
	 */
	protected void sureBtnClick() {
		handler.sendMessage(handler.obtainMessage(0));

		if (wordStudyPlan != null) {
			wordStudyPlan.setLearning(wordBookIsChecked);
			wordStudyPlan.savePlan(this);
			LogUtil.d("点击确定按钮,wordStudyPlan:" + wordStudyPlan.toString() + ",wordBookIsChecked=" + wordBookIsChecked);
		}

		if (phraseStudyPlan != null) {
			phraseStudyPlan.setLearning(phraseBookIsChecked);
			phraseStudyPlan.savePlan(this);
			LogUtil.d("点击确定按钮,phraseStudyPlan:" + phraseStudyPlan.toString() + ",phraseBookIsChecked=" + phraseBookIsChecked);
		}

		DataManager.getInstance().resetStudyPlan(this);
		handler.sendMessage(handler.obtainMessage(1));
	}

	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		if (requestCode == StaticData.requestCode && resultCode == StaticData.resultCode) {
			StudyPlan newStudyPlan = (StudyPlan) data.getSerializableExtra("studyPlan");
			if (newStudyPlan.getBookType() == AppPreference.BookType.BOOK_WORD) {
				wordStudyPlan = newStudyPlan;
				initWordStudyPlan();
			} else {
				phraseStudyPlan = newStudyPlan;
				initPhraseStudyPlan();
			}
			update();
		}
	};

	protected void showHintDialog() {
		View view = LayoutInflater.from(this).inflate(R.layout.view_dialog_normal, null);
		TextView content = (TextView) view.findViewById(R.id.dialog_content);
		LinearLayout ll = (LinearLayout) view.findViewById(R.id.two_btn_layout);
		ll.setVisibility(View.GONE);
		String contentText = "1. 可以为单词和词组分别设置学习计划，然后组合在一起学习。\n\n2. 原“单词+词组”书已下架。购买了这类组合的用户，现在可免费学习相应词组书，原先的学习进度我们已帮您智能保存和转换。";
		content.setText(contentText);
		Button getIt = (Button) view.findViewById(R.id.got_it);
		getIt.setVisibility(View.VISIBLE);
		final MyDialog infoDialog = new MyDialog(this, view, R.style.mydialog, false);
		infoDialog.setCanceledOnTouchOutside(true);
		infoDialog.show();
		getIt.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				infoDialog.dismiss();
			}
		});
	}
}
